<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateTableAdmins extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->integerIncrements('id');
            $table->string('username',50)->default('')->nullable(false)->unique()->comment('管理员账号');
            $table->string('password')->default('')->nullable(false)->comment('管理员密码');
            $table->text('token')->nullable(false)->comment('用户登录凭证');
            $table->integer('roleid')->default(-1)->nullable(false)->comment('管理员角色ID,为0时是超级管理员');
            $table->softDeletes();
            $table->timestamps();
        });
        DB::statement("alter table `admins` comment '后台管理员列表' ");

        Schema::create('admin_roles', function (Blueprint $table) {
            $table->integerIncrements('id');
            $table->string('rolename',50)->default('')->nullable(false)->unique()->comment('角色名称');
            $table->softDeletes();
            $table->timestamps();
        });
        DB::statement("alter table `admin_roles` comment '后台管理员角色列表' ");

        Schema::create('admin_modules', function (Blueprint $table) {
            $table->integerIncrements('id');
            $table->string('modname',50)->default('')->nullable(false)->comment('模块名称');
            $table->string('modspace',50)->default('')->nullable(false)->comment('模块所属控制器');
            $table->softDeletes();
            $table->timestamps();
        });
        DB::statement("alter table `admin_modules` comment '后台模块' ");

        Schema::create('admin_navs', function (Blueprint $table) {
            $table->integerIncrements('id');
            $table->integer('moid')->default(0)->nullable(false)->comment('模块ID');
            $table->string('navname',50)->default('')->nullable(false)->comment('菜单名称');
            $table->string('navaction',50)->default('')->nullable(false)->comment('菜单操作名称');
            $table->softDeletes();
            $table->timestamps();
        });
        DB::statement("alter table `admin_navs` comment '后台模块菜单表' ");

        Schema::create('admin_powers', function (Blueprint $table) {
            $table->integerIncrements('id');
            $table->string('powername',50)->default('')->nullable(false)->comment('权限名称');
            $table->string('path',50)->default('')->nullable(false)->comment('所在页面路径');
            $table->integer('navid')->default(0)->nullable(false)->comment('权限所属菜单ID');
            $table->unsignedTinyInteger('powertype')->default(0)->nullable(false)
                  ->comment('权限类型,0代表查看,1代表添加,2代表编辑,3代表审核,4代表删除,5导出,6导入');
            $table->unique(['navid', 'powertype']);
            $table->softDeletes();
            $table->timestamps();
        });
        DB::statement("alter table `admin_powers` comment '权限列表' ");

        Schema::create('role_powers', function (Blueprint $table) {
            $table->integer('pid')->default(0)->nullable(false)->comment('权限ID');
            $table->integer('roleid')->default(0)->nullable(false)->comment('角色ID');
            $table->primary(['pid', 'roleid']);
            $table->softDeletes();
            $table->timestamps();
        });
        DB::statement("alter table `role_powers` comment '角色权限列表' ");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admins');
        Schema::dropIfExists('admin_roles');
        Schema::dropIfExists('admin_modules');
        Schema::dropIfExists('admin_powers');
        Schema::dropIfExists('admin_navs');
        Schema::dropIfExists('role_powers');
    }
}
